<?php

namespace MemLib;

use \MemFrameWork\Mem;
use \MemLib\Log;
use \MemLib\Pdb;

/**
 * 功能基类
 *
 * @version 1.0
 * @author 张云江
 */
class FuncBase
{
    /**
     * 请求端类型
     * @var double
     */
    protected $c_type;

    /**
     * 请求端软件版本
     * @var double
     */
    protected $c_ver;

    /**
     * 建立功能类
     * @param mixed $c_type 请求端类型
     * @param mixed $c_ver 请求端软件版本
     */
    public function __construct($c_type = 0, $c_ver = 0)
    {
        $this->c_type = (float)$c_type;
        $this->c_ver = (float)$c_ver;
    }
    /**
     * 国内
     */
    const TEAM_TYPE_DOMESTIC = 0;
    /**
     * 国外
     */
    const TEAM_TYPE_ABROAD = 1;
    /**
     * 所有类型强转字符串
     * @param array $array 
     */
    private function convertToString(&$array)
    {
        foreach ($array as $key => &$obj) {
            if (is_array($obj)) {
                $this->convertToString($obj);
            } else {
                if (!is_string($obj))
                    $obj = (string)$obj;
                if (
                    $obj == "0000-00-00 00:00:00" ||
                    $obj == "1970-01-01 08:00:00" ||
                    $obj == "1970-01-01 00:00:00"
                )
                    $obj = "";
                $array[$key] = $obj;
            }
        }
    }
    /**
     * 获取返回信息的错误描述
     * @param mixed $data 返回的数据
     * @return mixed
     */
    public static function getErrorWithReturnData($data)
    {
        return $data["error"];
    }
    /**
     * 获取状态码
     * @param mixed $data 返回的数据
     * @return mixed
     */
    public static function getCodeWithReturnData($data)
    {
        return $data["code"];
    }
    /**
     * 获取返回的数据
     * @param mixed $data 返回的数据
     * @return mixed
     */
    public static function getDataWithReturnData($data)
    {
        return $data["data"];
    }
    /**
     * 返回数据
     * 接口函数返回json说明:
     * {
     * "code":"0",状态码
     * "error":"",错误描述
     * "data":"",返回的实际数据
     * }
     * @param int $code 状态码
     * @param string $error 错误描述
     * @param mixed $data 数据
     * @return array
     */
    public function returnData($code, $error, $data = array())
    {
        $this->convertToString($data);
        $datastr = array(
            "code" => $code . "",
            "error" => $error,
            "data" => $data
        );
        //        \Mlog::info(json_encode($datastr, JSON_UNESCAPED_UNICODE));
        return $datastr;
    }
    /**
     * 返回非指定字段名称的数据
     * @param mixed $fieldarray 字段名称集合
     * @param mixed $data 数据(单个)
     * @return array|null
     */
    public function outDataByField_Opposite($fieldarray, $data)
    {
        if (empty($data))
            return null;
        $newdata = array();
        foreach (array_keys($data) as $field) {
            foreach ($fieldarray as $outfield) {
                if ($outfield == $field)
                    goto next;
            }
            $newdata[$field] = $data[$field];
            next:
            continue;
        }
        return $newdata;
    }
    /**
     * 过滤非显示的字段
     * @param mixed $fieldarray 要过滤的字段列表
     * @param mixed $datalist 数据集合
     * @return array
     */
    public function outDataListByField_Opposite($fieldarray, $datalist)
    {
        $newdatalist = array();
        foreach ($datalist as $data) {
            $newdatalist[] = $this->outDataByField_Opposite($fieldarray, $data);
        }
        return $newdatalist;
    }
    /**
     * 返回指定字段名称的数据
     * @param array $fieldarray 字段名称集合
     * @param array $data 数据(单个)
     * @return array|null
     */
    public function outDataByField($fieldarray, $data)
    {
        $newdata = array();
        foreach ($fieldarray as $field) {
            if (!array_key_exists($field, $data)) {
                $newdata[$field] = "";
                continue;
            }
            $newdata[$field] = $data[$field];
        }
        return $newdata;
    }
    /**
     * 过滤要显示的字段
     * @param mixed $fieldarray 要过滤的字段列表
     * @param mixed $datalist 数据集合
     * @return array
     */
    public function outDataListByField($fieldarray, $datalist)
    {
        $newdatalist = array();
        foreach ($datalist as $data) {
            $newdatalist[] = $this->outDataByField($fieldarray, $data);
        }
        return $newdatalist;
    }
    /**
     * 获取当前日期
     * @return string
     */
    protected function getCurrentDay()
    {
        return date("Y-m-d");
    }
    /**
     * 获取当前日期与时间
     * @return string
     */
    protected function getCurrentTime()
    {
        return date("Y-m-d H:i:s");
    }
}
